﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmMatrizDecision.aspx.cs" Inherits="sistema_cotizacion_frmMatrizDecision" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <link href="../../kendoui.web/content/shared/styles/examples-offline.css" rel="stylesheet"/>
    <link href="../../kendoui.web/styles/kendo.common.min.css" rel="stylesheet"/>
    <link href="../../kendoui.web/styles/kendo.default.min.css" rel="stylesheet"/>
    <style type="text/css">
        #grdMatrizDecisiones th.k-header { text-align: center }
        .style1
        {
            height: 20px;
        }
        .style2
        {
            height: 39px;
        }
    </style>

    <script src="../../kendoui.web/js/jquery.min.js" type="text/javascript"></script>
    <script src="../../kendoui.web/js/kendo.web.min.js" type="text/javascript"></script>
    <script src="../../kendoui.web/content/shared/js/console.js" type="text/javascript"></script>
</head>
<body>
    <div id="frmContainer" class="k-content">
    <table border="0" width="100%">
    <tr>
        <td colspan="8" align="center"><h2>ACTUALIZAR MATRIZ DE DECISIÓN </h2></td>
    </tr>
    <tr>
        <td colspan="8" align="center" class="style1"></td>
    </tr>
    <tr>
        <td>Aseguradora:</td>
        <td><input id="cboAseguradora" style="width: 300px" /></td>
        <td>&nbsp;</td>
    </tr>
        <tr>
        <td>Tipo de Seguro:</td>
        <td><input id="cboTipoSeguro" style="width: 300px" /></td>
        <td>&nbsp;</td>
        <td>Ramo:</td>
        <td><input id="cboRamo" style="width: 300px" /></td>
        <td>&nbsp;</td>
        <td><button class="k-button" id="btnBuscar" style="width:160px;"><span class="k-icon k-i-search"></span>&nbsp;Buscar</button></td>
        <td>&nbsp;</td>
        </tr>
    <tr>
        <td colspan="8" align="center">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="8" align="center" class="style2"><form name="form1" action=""><div id="grdMatrizDecisiones"></div></form></td>
    </tr>
    <tr>
        <td colspan="8" align="center">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="8" align="center">
            <button class="k-button" id="btnNuevo" style="width:160px;"><span class="k-icon k-i-plus"></span>&nbsp;Nuevo</button>
            <button class="k-button" id="btnAnular" style="width:160px;"><span class="k-icon k-i-cancel"></span>&nbsp;Anular</button>
            <button class="k-button" id="btnSalir" style="width:160px;"><span class="k-icon k-i-close"></span>&nbsp;Salir</button>
        </td>
    </tr>
    </table>


    <script type="text/javascript">

        $(document).ready(function () {

            var cboAseguradora = $("#cboAseguradora").kendoComboBox({
                filter: "startswith",
                placeholder: "Seleccionar Aseguradora...",
                dataTextField: "dAseguradora",
                dataValueField: "cAseguradora",
                dataSource: {
                    transport: {
                        read: {
                            url: "http://localhost/Gra.Seguros.Servicio/MatrizDecision.svc/listarAseguradoras",
                            data: {
                                Accept: "application/json"
                            }
                        }
                    }
                }
            }).data("kendoComboBox");


            var cboTipoSeguro = $("#cboTipoSeguro").kendoComboBox({
                change: onChange_cboTipoSeguro,
                filter: "startswith",
                placeholder: "Seleccionar tipo de seguro...",
                dataTextField: "dTipoSeguro",
                dataValueField: "cTipoSeguro",
                dataSource: {
                    transport: {
                        read: {
                            url: "http://localhost/Gra.Seguros.Servicio/CotizacionSeguro.svc/listarTipoSeguros",
                            data: {
                                Accept: "application/json"
                            }
                        }
                    }
                }
            }).data("kendoComboBox");


            var existe_cboRamo = false;
            function onChange_cboTipoSeguro() {
                existe_cboRamo = true;
                $("#cboRamo").text("");
                $("#cboRamo").val("");
                var cboRamo = $("#cboRamo").kendoComboBox({
                    filter: "startswith",
                    placeholder: "Seleccionar ramo...",
                    dataTextField: "dRamo",
                    dataValueField: "cRamo",
                    dataSource: {
                        transport: {
                            read: {
                                url: "http://localhost/Gra.Seguros.Servicio/CotizacionSeguro.svc/listarRamos/" + cboTipoSeguro.value(),
                                data: {
                                    Accept: "application/json"
                                }
                            }
                        }
                    }
                }).data("kendoComboBox");
            }

            function fRight(str, n) {
                if (n <= 0)
                    return "";
                else if (n > String(str).length)
                    return str;
                else {
                    var iLen = String(str).length;
                    return String(str).substring(iLen, iLen - n);
                }
            }


            function fLeft(str, n) {
                if (n <= 0)
                    return "";
                else if (n > String(str).length)
                    return str;
                else
                    return String(str).substring(0, n);
            }

            $("#btnBuscar").click(function () {

                var _cboAseguradora = "0";
                var _cboTipoSeguro = "0";
                var _cboRamo = "0";

                if (cboAseguradora.value() != "") _cboAseguradora = cboAseguradora.value();
                if (cboTipoSeguro.value() != "") _cboTipoSeguro = cboTipoSeguro.value();
                if (existe_cboRamo) {
                    if ($("#cboRamo").val() != "") _cboRamo = $("#cboRamo").val();
                }

                var grdMatrizData;

                grdMatrizData = new kendo.data.DataSource(
                {
                    transport: {
                        read: {
                            url: "http://localhost/Gra.Seguros.Servicio/MatrizDecision.svc/listarMatriz/" + _cboTipoSeguro + "/" + _cboRamo + "/" + _cboAseguradora,
                            data: {
                                Accept: "application/json"
                            }
                        }
                    },
                    pageSize: 10
                });




                $("#grdMatrizDecisiones").kendoGrid({
                    dataSource: grdMatrizData,
                    groupable: true,
                    sortable: true,
                    pageable: {
                        refresh: true,
                        pageSizes: true
                    },
                    selectable: "multiple",
                    dataBound: function () {
                        var grid = this;
                        grid.table.find("tr").find("td:first input")
                        .change(function (e) {
                            var checkbox = $(this);
                            var selected = grid.table.find("tr").find("td:first input:checked").closest("tr");
                            grid.clearSelection();
                            if (selected.length) {
                                grid.select(selected);
                            }
                        });
                    },
                    columns: [
                        { width: 16, field: "Matriz", template: '<input id="${cMatrizc}" type="checkbox" />', filterable: false, sortable: false },
                    //                        headerTemplate: '<input id="checkAll" type="checkbox" onclick="ChequearTodos(this);" />'                         
                        { width: 90, field: "dTipoSeguro", title: "Tipo Seguro" },
                        { width: 90, field: "dRamo", title: "Ramo" },
                        { width: 90, field: "dAseguradora", title: "Aseguradora" },
                        { width: 30, field: "nCantidad", title: "Nro Reglas" }
                    ]
                });

            });



            $("#btnSalir").click(function () {
                document.location.href = "../../Default.aspx";
            });

            $("#btnNuevo").click(function () {
                document.location.replace("frmRegistroMatrizDecision.aspx");
            });


          $("#btnAnular").click(function () {
                var haySel = false;
                var strXML = '$MatrizDecision@';
                for (var i = 0; i < document.forms["form1"].elements.length; i++) {
                    var elemento = document.forms[0].elements[i];
                    if (elemento.type == "checkbox" && elemento.id != "checkAll" && elemento.checked) {
                        haySel = true;
                        strXML = strXML + '$Matriz@$cMatriz@' + elemento.id + '$-cMatriz@$-Matriz@';
                    }
                }
                strXML = strXML + '$-MatrizDecision@';

                if (haySel) {
                    $.ajax({
                        type: "GET",
                        url: "http://localhost/Gra.Seguros.Servicio/MatrizDecision.svc/anularMatriz/" + strXML,
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            alert("Anulación exitosa!!!.");
                            $("#btnBuscar").click();
                        },
                        error: function (xhr) {
                            alert(xhr.responseText);
                        }
                    });
                }
                else {
                    alert("Seleccione alguna cotización a anular.");
                }
            });



        });

    </script>    
   </div>
</body>
</html>
